using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace teavch.Utilities.Electrical.Design.Gui.LineDiagram
{
	public partial class LighteningArrestor : ElectricalSymbol
	{

		private Matrix matrix = null;

		private float _topHandRatio = 1f;
		private float _bottomHandRatio = 3f;
		private float _isolatorGapRatio = 1f;
		private float _isolatorHookRatio = .1f;
		private SymbolOrientation _orientation = SymbolOrientation.Verticle;

		public LighteningArrestor()
		{
			InitializeComponent();
		}

		[Category( "Graphical Symbols for Single Line Diagrams" ), Description( "Get or set the Top Link Line ration of the top node of the Isolator" )]
		public float TopHandRatio
		{
			get { return this._topHandRatio; }
			set
			{
				this._topHandRatio = value;
				this.Refresh();
			}
		}

		[Category( "Graphical Symbols for Single Line Diagrams" ), Description( "Get or set the Bottom Link Line ration of the bottom node of the Isolator" )]
		public float BottomHandRatio
		{
			get { return this._bottomHandRatio; }
			set
			{
				this._bottomHandRatio = value;
				this.Refresh();
			}
		}

		[Category( "Graphical Symbols for Single Line Diagrams" ), Description( "Get or set the ratio of isolator bar hucks of the Isolator" )]
		public float IsolatorHookRatio
		{
			get { return this._isolatorHookRatio; }
			set
			{
				this._isolatorHookRatio = value;
				this.Refresh();
			}
		}

		[Category( "Graphical Symbols for Single Line Diagrams" ), Description( "Get or set the Gap ration between isolator bar and nodes of the Isolator" )]
		public float IsolatorGapRatio
		{
			get { return this._isolatorGapRatio; }
			set
			{
				this._isolatorGapRatio = value;
				this.Refresh();
			}
		}

		[Category( "Graphical Symbols for Single Line Diagrams" ), Description( "Get or set the element orientation of the Isolator" )]
		public SymbolOrientation Orientation
		{
			get { return this._orientation; }
			set
			{
				this._orientation = value;
				this.Refresh();
			}
		}

		protected override void OnPaint( PaintEventArgs e )
		{
			base.OnPaint( e );
			this.Paint( e );
		}

		protected override void OnResize( EventArgs e )
		{
			base.OnResize( e );
			ResizeNow( e );
		}

		private void ResizeNow( EventArgs ea )
		{
			this.Refresh();
		}

		private new void Paint( PaintEventArgs pea )
		{
			System.Drawing.Drawing2D.PathGradientBrush _gBrush = null;
			System.Drawing.Drawing2D.GraphicsPath _gpath = new System.Drawing.Drawing2D.GraphicsPath();

			float c1x1 = 0, c1y1 = 0;
			float c2x1 = 0, c2y1 = 0;
			float h1x1 = 0, h1y1 = 0;
			float h2x1 = 0, h2y1 = 0;
			float c1size = 0;
			float h1size = 0;

			float l1x1 = 0, l1y1 = 0, l1x2 = 0, l1y2 = 0;
			float l2x1 = 0, l2y1 = 0, l2x2 = 0, l2y2 = 0;
			float l3x1 = 0, l3y1 = 0, l3x2 = 0, l3y2 = 0;

			c1size = this.SymbolSize * .3f;

			l1x1 = c1size / 2;
			l1y1 = 0;
			l1x2 = l1x1;
			l1y2 = c1size * this._topHandRatio;

			c1x1 = 0;
			c1y1 = l1y2;

			c2x1 = 0;
			c2y1 = l1y2 + c1size + ( c1size * this._isolatorGapRatio );

			l2x1 = l1x1;
			l2y1 = c2y1 + c1size;
			l2x2 = l2x1;
			l2y2 = l2y1 + ( c1size * this._bottomHandRatio );

			if ( this.State == SymbolState.Off )
			{
				l3x1 = c2x1;
				l3y1 = c2y1 - ( ( c1size * this._isolatorGapRatio ) / 2 );
				l3x2 = c1size;
				l3y2 = l3y1;
			}
			else
			{
				l3x1 = c2x1;
				l3y1 = c2y1 - ( ( c1size * this._isolatorGapRatio ) / 2 );
				l3x2 = c1size;
				l3y2 = l3y1;
			}

			h1x1 = l3x1;
			h1y1 = l3y1 - ( c1size * this._isolatorHookRatio / 2 );
			h1size = ( c1size * this._isolatorHookRatio );
			l3x1 += h1size;

			h2x1 = l3x2 - ( c1size * this._isolatorHookRatio );
			h2y1 = h1y1;
			l3x2 -= h1size;

			if ( this.Fill == false )
			{
				//_gpath.AddArc( h1x1, h1y1, h1size, h1size, 0, 360 );
				//_gpath.AddArc( h2x1, h2y1, h1size, h1size, 0, 360 );
				_gpath.CloseFigure();
				_gpath.AddArc( c1x1, c1y1, c1size, c1size, 0, 360 );
				_gpath.AddArc( c2x1, c2y1, c1size, c1size, 0, 360 );
			}
			else
			{
				_gpath.AddArc( c1x1, c1y1, c1size, c1size, 0, 360 );
				_gBrush = new System.Drawing.Drawing2D.PathGradientBrush( _gpath );
				_gBrush.CenterColor = this.Parent.BackColor;
				_gBrush.SurroundColors = new Color[] { this.State == SymbolState.On ? Color.Red : Color.Red };
				_gBrush.WrapMode = WrapMode.TileFlipY;
				pea.Graphics.FillPath( _gBrush, _gpath );

				_gpath.CloseFigure();
				_gpath.AddArc( c2x1, c2y1, c1size, c1size, 0, 360 );
				_gBrush = new System.Drawing.Drawing2D.PathGradientBrush( _gpath );
				_gBrush.CenterColor = this.Parent.BackColor;
				_gBrush.SurroundColors = new Color[] { this.State == SymbolState.On ? Color.Red : Color.Lime };
				_gBrush.WrapMode = WrapMode.TileFlipY;
				pea.Graphics.FillPath( _gBrush, _gpath );

				//_gpath.CloseFigure();
				//_gpath.AddArc( h1x1, h1y1, h1size, h1size, 0, 360 );
				//_gBrush = new System.Drawing.Drawing2D.PathGradientBrush( _gpath );
				//_gBrush.CenterColor = this.Parent.BackColor;
				//_gBrush.SurroundColors = new Color[] { this.State == SymbolState.On ? Color.Red : Color.Lime };
				//_gBrush.WrapMode = WrapMode.TileFlipY;
				//pea.Graphics.FillPath( _gBrush, _gpath );

				//_gpath.CloseFigure();
				//_gpath.AddArc( h2x1, h2y1, h1size, h1size, 0, 360 );
				//_gBrush = new System.Drawing.Drawing2D.PathGradientBrush( _gpath );
				//_gBrush.CenterColor = this.Parent.BackColor;
				//_gBrush.SurroundColors = new Color[] { this.State == SymbolState.On ? Color.Red : Color.Lime };
				//_gBrush.WrapMode = WrapMode.TileFlipY;
				//pea.Graphics.FillPath( _gBrush, _gpath );
			}
			//_gpath.CloseFigure();
			//_gpath.AddLine( l3x1, l3y1, l3x2, l3y2 );
			_gpath.CloseFigure();
			_gpath.AddLine( l1x1, l1y1, l1x2, l1y2 );
			_gpath.CloseFigure();
			_gpath.AddLine( l2x1, l2y1, l2x2, l2y2 );
			_gpath.CloseFigure();
			_gpath.AddLine( 0, l2y2, c1size, l2y2 );
			_gpath.CloseFigure();
			_gpath.AddLine( ( c1size * .2f ), l2y2 + 2, c1size - ( c1size * .2f ), l2y2 + 2 );
			_gpath.CloseFigure();
			_gpath.AddLine( ( c1size * .4f ), l2y2 + 4, c1size - ( c1size * .4f ), l2y2 + 4 );

			if ( this._orientation == SymbolOrientation.Horizontal )
			{
				this.matrix = new Matrix();
				this.matrix.Rotate( 90, MatrixOrder.Prepend );
				pea.Graphics.Transform = this.matrix;
			}

			this.Height = ( int ) Math.Ceiling( l2y2 + 9 );
			this.Width = ( int ) Math.Ceiling( c1size + 1 );

			pea.Graphics.DrawPath( this.Pen, _gpath );
		}
	}
}